Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
is there a function "amongst" or equivalent in lodash or JS?

I want the function "amongst" or "within" in lodash or JS. The opposite already exists, it is called "includes". Example :

_(['a', 42]).includes(42)

I would like to write

_(42).amongst(['a', 42])

Any clues ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your amongst / within function is a version of _.includes() with the search value 1st and the collection 2nd, so you can create one easily by flipping _.includes():

const amongst = _.flip(_.includes)

console.log(amongst(42, ['a', 42]))
console.log(amongst('b', ['a', 42]))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

And you can use _.mixin() to add it to lodash:

const amongst = _.flip(_.includes)

_.mixin({ amongst });


console.log(_(42).amongst(['a', 42]))
console.log(_('b').amongst(['a', 42]))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

In vanillaJS you can create a function that does the same thing:

const amongst = (value, collection) => collection.includes(value)

console.log(amongst(42, ['a', 42]))
console.log(amongst('b', ['a', 42]))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!